import {
  AmbientLight,
  AxesHelper,
  BoxGeometry,
  DirectionalLight,
  Fog,
  GridHelper,
  Group,
  Mesh,
  MeshBasicMaterial,
  MeshPhysicalMaterial,
  PerspectiveCamera,
  PlaneGeometry,
  PMREMGenerator,
  Scene,
  SphereGeometry,
  Vector3,
  WebGLRenderer,
} from "three";
import {
  GLTFLoader,
  MTLLoader,
  OBJLoader,
  OrbitControls,
  RoomEnvironment,
} from "three/examples/jsm/Addons.js";
import GUI from "three/examples/jsm/libs/lil-gui.module.min.js";
import Stats from "three/examples/jsm/libs/stats.module.js";

export default class Y3DEngine {
  constructor() {
    this.init();
    this.animate();
  }

  init() {
    this.initRenderer();
    this.initScene();
    this.initCamera();
    this.initControls();
    this.initObjects();
    this.initLight();
    this.initHelper();
    this.initGui();
    this.initStats();
    this.initEvents();
  }

  initObjects() {
    // const plane = new PlaneGeometry(10, 10);
    // const material = new MeshBasicMaterial({ color: 0xffffff });
    // const mesh = new Mesh(plane, material);
    // this.scene.add(mesh);
    // const mtlLoader = new MTLLoader();
    // mtlLoader.load("../assets/混凝土万能试验机.mtl", (materials) => {
    //   materials.preload();
    //   const objLoader = new OBJLoader();
    //   objLoader.setMaterials(materials);
    //   objLoader.load("../assets/混凝土万能试验机.obj", (object) => {
    //     this.scene.add(object);
    //   });
    // });
    const loader = new GLTFLoader();
    loader.load("../assets/钢筋力学万能试验机.glb", (gltf) => {
      const model = gltf.scene;
      // model.scale.set(0.01, 0.01, 0.01);
      this.scene.add(model);
    });
    // const box = new BoxGeometry(2, 3);
    // const material = new MeshPhysicalMaterial({
    //   color: 0x00ff00,
    //   // roughness: ,
    //   metalness: 0,
    // });
    // const mesh = new Mesh(box, material);
    // this.scene.add(mesh);
    // {
    //   const material = new MeshBasicMaterial({ color: 0x00ff00 });
    //   const mesh = new Mesh(box, material);
    //   mesh.position.set(3, 0, 0);
    //   this.scene.add(mesh);
    // }
  }

  initGui() {
    this.gui = new GUI();
    this.gui.title("Y3D Engine");
  }

  initStats() {
    this.stats = new Stats();
    document.body.appendChild(this.stats.dom);
  }

  initEvents() {
    window.addEventListener("resize", this.onWindowResize);
  }

  onWindowResize = () => {
    this.camera.aspect = this.aspect;
    this.camera.updateProjectionMatrix();
    this.renderer.setSize(this.width, this.height);
  };

  initLight() {
    this.lights = new Group();
    // const light = new AmbientLight(0xffffff, 0.3);
    // this.lights.add(light);
    // const light2 = new DirectionalLight(0xffffff, 0.7);
    // light2.position.set(1, 0, 0);
    // this.lights.add(light2);
    // let light3 = light2.clone();
    // light3.intensity = 0.5;
    // light3.position.set(-1, 0, 0);
    // this.lights.add(light3);

    // light3.intensity = 0.7;
    // light3 = light2.clone();
    // light3.position.set(0, 1, 0);
    // this.lights.add(light3);

    // light3 = light2.clone();
    // light3.intensity = 0.3;
    // light3.position.set(0, -1, 0);
    // this.lights.add(light3);

    this.cameraLight = new DirectionalLight(0xffffff, 0.3);
    // this.lights.add(this.cameraLight);
    // this.scene.add(this.lights);
  }

  initHelper() {
    this.helpers = new Group();
    const gridHelper = new GridHelper(10, 10);
    const axesHelper = new AxesHelper(5);
    this.helpers.add(axesHelper);
    this.helpers.add(gridHelper);
    this.scene.add(this.helpers);
  }

  initControls() {
    this.controls = new OrbitControls(this.camera, this.renderer.domElement);
  }

  initCamera() {
    this.camera = new PerspectiveCamera(45, this.aspect, 0.1, 20000);
    this.camera.position.set(0, 0, 10);
    this.camera.lookAt(new Vector3());
  }

  initScene() {
    this.scene = new Scene();

    const pmremGenerator = new PMREMGenerator(this.renderer);
    this.scene.environment = pmremGenerator.fromScene(
      new RoomEnvironment(),
      0.04
    ).texture;
    // this.scene.fog = new Fog(0x312c42, 5, 8000);
  }

  initRenderer() {
    this.renderer = new WebGLRenderer({
      antialias: true,
      alpha: true,
    });
    this.renderer.setPixelRatio(window.devicePixelRatio);
    this.renderer.setSize(this.width, this.height);
    this.renderer.setClearColor(0x312c42, 1);
    document.body.appendChild(this.renderer.domElement);
  }

  get width() {
    return window.innerWidth;
  }

  get height() {
    return window.innerHeight;
  }

  get aspect() {
    return this.width / this.height;
  }

  animate = () => {
    requestAnimationFrame(this.animate);
    this.stats.update();
    this.cameraLight.position.copy(this.camera.position).normalize();
    this.renderer.render(this.scene, this.camera);
  };
}

window.addEventListener("DOMContentLoaded", () => {
  const y3engine = new Y3DEngine();
  window.y3engine = y3engine;
});
